package com.tencent.timsdk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.tencent.TIMUserProfile;
import com.tencent.timsdk.R;
import com.tencent.timsdk.model.Message;

import java.util.List;

/**
 * 聊天界面adapter
 */
public class ChatAdapter extends ArrayAdapter<Message> {

    private final String TAG = "ChatAdapter";

    private int resourceId;
    private View view;
    private ViewHolder viewHolder;

    private ChatAdapterLayoutCallback callback;

    /**
     * Constructor
     *
     * @param context  The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects  The objects to represent in the ListView.
     */
    public ChatAdapter(Context context, int resource, List<Message> objects, ChatAdapterLayoutCallback callback) {
        super(context, resource, objects);
        resourceId = resource;
        this.callback = callback;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView != null) {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }
        final Message data = getItem(position);
        if (callback != null){
            callback.configChatAdapterLayout(data, viewHolder);
        }
        if (position < getCount()) {
            data.showMessage(viewHolder, getContext());
        }
        return view;
    }

    public interface ChatAdapterLayoutCallback {
        void configChatAdapterLayout(Message message, ViewHolder viewHolder);
    }

    public static class ViewHolder extends BaseViewHolder{
        public LinearLayout messageContainer;
        public RelativeLayout leftMessage;
        public RelativeLayout rightMessage;
        public RelativeLayout leftPanel;
        public RelativeLayout rightPanel;
        public ProgressBar sending;
        public ImageView error;
        public ImageView leftAvatar;
        public ImageView rightAvatar;
        public TextView sender;
        public TextView systemMessage;
        public TextView rightDesc;

        public ViewHolder(View view) {
            super(view);
            messageContainer = (LinearLayout) view.findViewById(R.id.ll_message_container);
            leftMessage = (RelativeLayout) view.findViewById(R.id.leftMessage);
            leftAvatar = (ImageView) view.findViewById(R.id.leftAvatar);
            rightMessage = (RelativeLayout) view.findViewById(R.id.rightMessage);
            rightAvatar = (ImageView) view.findViewById(R.id.rightAvatar);
            leftPanel = (RelativeLayout) view.findViewById(R.id.leftPanel);
            rightPanel = (RelativeLayout) view.findViewById(R.id.rightPanel);
            sending = (ProgressBar) view.findViewById(R.id.sending);
            error = (ImageView) view.findViewById(R.id.sendError);
            sender = (TextView) view.findViewById(R.id.sender);
            systemMessage = (TextView) view.findViewById(R.id.systemMessage);
            rightDesc = (TextView) view.findViewById(R.id.rightDesc);
        }
    }
}
